package com.example.gestureactivity;

import com.example.gestureactivity.utils.WebViewUtils;

import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.Button;
import android.widget.RelativeLayout;

public class WebActivity extends BaseActivity implements OnClickListener {

	private static final String TAG = "WebActivity";
	private WebView webView;
	private int fontSize = 0;

	WebSettings settings;
	private Button mAddBtn;
	private Button mReduceBtn;
	private SharedPreferences sp;
	private RelativeLayout mLayout;

	@Override
	protected void onCreate(Bundle arg0) {
		super.onCreate(arg0);
		setContentView(R.layout.activity_webview);
		sp = getSharedPreferences("settings", MODE_PRIVATE);

		initView();
	}

	private void initView() {
		mLayout = (RelativeLayout) findViewById(R.id.layout_webview);

		webView = (WebView) findViewById(R.id.webView);
		webView.loadUrl("http://mail.126.com");
		webView.setWebViewClient(new MyWebViewClient());
		settings = webView.getSettings();

		mAddBtn = (Button) findViewById(R.id.btn_add);
		mReduceBtn = (Button) findViewById(R.id.btn_reduce);
		mAddBtn.setOnClickListener(this);
		mReduceBtn.setOnClickListener(this);

		fontSize = sp.getInt("fontSize", 3);

		setFontSize();
		setListener();
	}

	@Override
	void onFlingFinished() {
		this.finish();

	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		int action = event.getAction();
		switch (action) {
		case MotionEvent.ACTION_DOWN:
			startX = event.getX();
			startY = event.getY();
			break;
		case MotionEvent.ACTION_MOVE:
			Log.i(TAG, "touch: x = " + event.getX() + ", y = " + event.getY());
			break;
		case MotionEvent.ACTION_UP:
		case MotionEvent.ACTION_CANCEL:
			float currentX = event.getX();
			float currentY = event.getY();
			float dx = currentX - startX;
			float dy = currentY - startY;
			Log.e(TAG, "dx = " + dx + "," + "|dx| = " + Math.abs(dx)
					+ ",|dy| = " + Math.abs(dy));
			if (dx > 0 && Math.abs(dx) > Math.abs(dy)) {
				// 右划，而且水平方向的位移大于竖直方向的位移
				onFlingFinished();
			}
			break;

		}
		return false;
	}

	@Override
	public void onClick(View v) {
		int id = v.getId();
		switch (id) {
		case R.id.btn_add:
			fontSize++;
			break;
		case R.id.btn_reduce:
			fontSize--;
			break;
		}

		setFontSize();
	}

	private void setFontSize() {
		if (fontSize <= 1) {
			fontSize = 1;
			mReduceBtn.setEnabled(false);
		} else if (fontSize >= 5) {
			fontSize = 5;
			mAddBtn.setEnabled(false);
		} else {
			mReduceBtn.setEnabled(true);
			mAddBtn.setEnabled(true);
		}

		Editor editor = sp.edit();
		editor.putInt("fontSize", fontSize);
		editor.commit();

		WebViewUtils.setFontSize(webView, fontSize);
	}

	private class MyWebViewClient extends WebViewClient {
		@Override
		public boolean shouldOverrideUrlLoading(WebView view, String url) {
			return super.shouldOverrideUrlLoading(view, url);
		}
	}

	private void setListener() {
		for (int i = 0; i < mLayout.getChildCount(); i++) {
			mLayout.getChildAt(i).setOnTouchListener(this);
		}
	}
}
